﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Windows_Explorer.Class
{
    public static class FileUtils
    {
        /// <summary>
        /// Create file with format txt, with empty value inside
        /// </summary>
        /// <returns></returns>
        public static bool createFileTxt(String fileName, String format){
            bool error = false;
            FileStream fout;
            String file = fileName + "." + format;
            try
            {
                if(File.Exists(file)){
                    fout = new FileStream(file, FileMode.Create);
                } else{
                    error = true;
                }
            }
            catch (IOException iException)
            {
                error = true;
            }
            return error;
        }

        /// <summary>
        /// Fill data into file with exists file.
        /// </summary>
        /// <returns>error</returns>
        public static bool fillDataToFile(String file, String text)
        {
            bool error = false;
            try
            {
                if (File.Exists(file))
                {
                    StreamWriter strFile = new StreamWriter(file, true);
                    strFile.WriteLine(text);
                }
                else
                {
                    error = true;
                }
            }
            catch (IOException ioException)
            {
                error = true;
            }
            return error;
        }

        /// <summary>
        /// Read the file and add it to dictionary.
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public static Dictionary<String, String> readDataFromFile(String file)
        {
            String line;
            Dictionary<String, String> result = new Dictionary<string, string>();
            StreamReader strFile = new StreamReader(file);
            while ((line = strFile.ReadLine()) != null)
            {
                String[] words = line.Split(',');
                result.Add(words[0],words[1]);
            }
            return result;
        }
        
    }
}
